//
//  TipsView.swift
//  ShouChong
//
//  Created by yw on 2024/8/11.
//

import UIKit

class TipsView: UIView {
    @IBOutlet weak var backgroundView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var contentLabel: UILabel!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var confirmBtn: UIButton!
    
    var confirmAction:((_:Int) -> Void)?
    var cancelAction:(() -> Void)?
    var showFeelingView:Bool = true
    @IBAction func cancelBtnClick(_ sender: UIButton) {
        self.hideToView()
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.prepare()
        generator.impactOccurred()
        self.cancelAction?()
    }
    
    
    @IBAction func confrimBtnClick(_ sender: UIButton) {
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.prepare()
        generator.impactOccurred()
        if self.showFeelingView {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                self.hideToView()
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                    let feelingView = FeelingView.createFeelingView { tag in
                        DPrint(tag)
                        self.confirmAction?(tag)
                    }
                    feelingView.show(.alert, backgoundTapDismissEnable: false)
                }
                
            }
        } else {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                self.hideToView()
                self.confirmAction?(0)
            }
        }
        

        
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.backgroundView.layer.cornerRadius = 20
        
        self.cancelBtn.layer.cornerRadius = 28
        self.confirmBtn.layer.cornerRadius = 28
        self.confirmBtn.backgroundColor = UIColor(named: GetMainColorTool.getMainColor())
        
    }
    
    class func createTipsView(title:String = "提示",content:String = "",showFeelingView:Bool = true,confirmAction:@escaping ((_:Int) -> Void),cancelAction:@escaping (() -> Void)) -> TipsView {
        let tipsViews = Bundle.main.loadNibNamed("TipsView", owner: nil)?.first as! TipsView
        tipsViews.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 280)
        tipsViews.confirmAction = confirmAction
        tipsViews.cancelAction = cancelAction
        tipsViews.showFeelingView = showFeelingView
        tipsViews.titleLabel.text = title
        tipsViews.contentLabel.text = content
        return tipsViews
    }
}
